package src;

import com.google.gdata.util.ServiceException;
import java.io.BufferedOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author administrador
 */
public class Updater
{
    public boolean update( String link )  throws MalformedURLException, FileNotFoundException, IOException, ServiceException
    {
        
        ComputeUsage c = new ComputeUsage();
        c.setData( "", "Starting update", "" );
        Degub.getInstance().Debug("Updating version starting..");

        java.io.FileOutputStream fos = new java.io.FileOutputStream( "QuicklyTranslator.jar" );
        java.io.BufferedOutputStream out = new BufferedOutputStream( fos, 1024 );

        InputStream in = new java.net.URL( link ).openStream();
        byte[] buf = new byte[ 4 * 1024 ]; // 4K buffer
        int bytesRead;
        int control = 0;
        while ( ( bytesRead = in.read( buf ) ) != -1 )
        {
            out.write( buf, 0, bytesRead );
            control +=bytesRead;
            Degub.getInstance().Debug( "                                                                   Getting new version.. " + (control/1024) + "kb" );
        }

        out.close();

        Degub.getInstance().DebugSameLine( "Congratulations..! Updated to the last version.." );

        WriteProprierts w = new WriteProprierts();
        w.write( WriteProprierts.VERSION, QuicklyTranslatorFrame.VERSION );
        c.setData( "null", "Update sucefull", "");

        return true;
    }
}